package com.giveu.signature.module.template.service.impl;

import com.giveu.signature.module.template.model.SignTemplateParam;
import com.giveu.signature.module.template.model.SignatureConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.giveu.common.exception.ResultRuntimeException;
import com.giveu.common.page.PageModel;
import com.giveu.common.page.QueryMap;
import com.giveu.signature.common.util.MapperUtil;
import com.giveu.signature.config.FeignProperties;
import com.giveu.signature.module.template.mapper.SignatureConfigMapper;
import com.giveu.signature.module.template.service.SignatureConfigService;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 签章信息配置 服务实现类
 * </p>
 *
 * @author zhaoruding
 * @since 2019-11-12
 */
@Service
public class SignatureConfigServiceImpl implements SignatureConfigService {
	
	@Autowired
	SignatureConfigMapper signatureConfigMapper;
	@Autowired
	FeignProperties feignProperties;

	@Override
	public SignatureConfig get(Long id) {
		SignatureConfig entity = signatureConfigMapper.selectByPrimaryKey(id);
		if(entity==null){
			return null;
		}
		if(StringUtils.isNotBlank(entity.getImageUrl())){
			entity.setImagePath(feignProperties.getFastdfsUrl()+entity.getImageUrl());
		}
		if(StringUtils.isNotBlank(entity.getKeystoreUrl())){
			entity.setKeystorePath(feignProperties.getFastdfsUrl()+entity.getKeystoreUrl());
		}
		return entity;
	}

	@Override
	public PageInfo<SignatureConfig> listByPage(QueryMap<SignatureConfig> queryMap) {
		PageModel page = queryMap.getPage();
        PageHelper.startPage(page);
        List<SignatureConfig> list = new ArrayList<>();
        try {
            list = signatureConfigMapper.selectList(queryMap.getEntity());
        } catch (Exception e) {
        	throw new ResultRuntimeException("数据查询异常",e);
        }
        return new PageInfo<>(list);
	}

	@Override
	public void save(SignatureConfig entity) {
		checkData(entity);
		if(entity.getId()==null){
			signatureConfigMapper.insertSelective(entity);
		}else{
			signatureConfigMapper.updateByPrimaryKeySelective(entity);
		}
	}

	private void checkData(SignatureConfig entity) {
		if(!entity.isModify()){
			SignatureConfig record = new SignatureConfig();
			record.setSignCode(entity.getSignCode());
			record.setSignVersion(entity.getSignVersion()==null?1:entity.getSignVersion());
			SignatureConfig res = MapperUtil.get(signatureConfigMapper, record);
			if(res!=null){
				if(res.getId()!=entity.getId()){
					throw new RuntimeException("签名编码存在重复");
				}
			}
		}
	}

	@Override
	public void delete(SignatureConfig entity) {
		signatureConfigMapper.delete(entity);
	}

	@Override
	public List<SignatureConfig> signObjects() {
		SignatureConfig record = new SignatureConfig();
		record.setSignStatus(1);
		return signatureConfigMapper.select(record);
	}
	
}
